import { Base } from '../base.js';

class Address extends Base {
	
	/**
	 * 获取默认地址
	 */
	getDefault() {
		var params = {
			url: 'v1/address/default'
		}
		return this.request(params);
	}
	
  /**
   * 收货地址列表
   */
	getList() {
		var params = {
			url: 'v1/address/list',
		}
		return this.request(params);
	}
	
	/**
	 * 根据地址id，获取地址详情
	 * @param {Object} id 地址id
	 */
	getDetail(id) {
		var params = {
			url: 'v1/address/detail?id='+id,
		}
		return this.request(params);
	}
	
	
	/**
	 * 新增收货地址
	 * @param {Object} addressInfo
	 */
	add(addressInfo) {
		var params = {
			type: 'POST',
			url: 'v1/address/add',
			data: {addressInfo: addressInfo}
		}
		return this.request(params);
	}
	
	/**
	 * 更新(编辑)收货地址
	 * @param {Object} addressInfo
	 */
	edit(addressInfo) {
		var params = {
			type: 'POST',
			url: 'v1/address/edit',
			data: {addressInfo: addressInfo}
		}
		return this.request(params);
	}
	
	/**
	 * 设置默认地址
	 * @param {Object} addressInfo
	 */
	setDefault(id) {
		var params = {
			type: 'POST',
			url: 'v1/address/setdefault',
			data: {id: id}
		}
		return this.request(params);
	}
	
	/**
	 * 删除收货地址
	 * @param {Object} addressInfo
	 */
	delete(id) {
		var params = {
			type: 'POST',
			url: 'v1/address/delete',
			data: {id: id}
		}
		return this.request(params);
	}
	
	/*是否为直辖市*/
	isCenterCity(name) {
	    var centerCitys=['北京市','天津市','上海市','重庆市'],
	        flag = centerCitys.indexOf(name) >= 0;
	    return flag;
	}
	
	/*
	*根据省市县信息组装地址信息
	* provinceName , province 前者为 微信选择控件返回结果，后者为查询地址时，自己服务器后台返回结果
	*/
	   setAddressInfo(res){
	    var province =res.provinceName || res.province,
	        city =res.cityName || res.city,
	        country =res.countyName || res.region,
	        detail =res.detailInfo || res.detail;
	    var totalDetail = city+country+detail;
	    //直辖市，取出省部分
	    if(!this.isCenterCity(province)) {
	        totalDetail = province+ totalDetail;
	    };
	    return totalDetail;
	  }
	
};

export { Address };